home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo1.zoo / demo / icon / cut.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  6.0 KB  |  286 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: cut.c,v 1.1 88/07/08 11:11:39 sau Exp $
  9.     $Source: /tmp/mgrsrc/demo/icon/RCS/cut.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/icon/RCS/cut.c,v $$Revision: 1.1 $";
  12.  
  13. /* save current cut buffer in a file */
  14.  
  15. #include <signal.h>
  16. #include "term.h"
  17.  
  18. #define TERM        "mgr"            /* name of valid terminal id */
  19.  
  20. #define RESTORE    0                /* restore previous icon */
  21. #define CLOSED        1
  22. #define OPEN        2
  23. #define WINK        3
  24.  
  25. #define ICON_CLOSED    "file_shut"
  26. #define ICON_OPEN        "file_open"
  27. #define ICON_WINK        "file_shutb"
  28. #define CMDNAME        "hm -s"    /* default command */
  29. #define SLOP            3            /* space around the window */
  30.  
  31. #define MENU_COUNT        (sizeof(menu)/sizeof(struct menu_entry))
  32. #define max(x,y)    ((x)>(y)?(x):(y))
  33. #define min(x,y)    ((x)<(y)?(x):(y))
  34. #define dprintf    if(debug)fprintf
  35.  
  36. int border;
  37. FILE *debug;
  38.  
  39. main(argc,argv)
  40.     int argc;
  41.     char **argv;
  42.     {
  43.     register int i;
  44.     char *getenv();
  45.     char *term = getenv ("TERM");
  46.    char *name = argv[0];
  47.    char *cmd;                /* command to pipe data to */
  48.     char line[80];            /* event input buffer */
  49.  
  50.     int x,y,wide,high;    /* main window size */
  51.     int w,h;                    /* icon size */
  52.     int max_w, max_h;        /* max icon size */
  53.     int ix, iy;                /* icon position */
  54.     int fw,fh,font;                /* font for vi window */
  55.    int got=0;
  56.    int shape = 1;            /* window reshape? */
  57.    FILE *test;                /* temporary file */
  58.  
  59.     int clean();
  60.  
  61.     if (getenv("DEBUG"))
  62.         debug = stderr;
  63.     else
  64.         debug = NULL;
  65.  
  66.     /* make sure environment is ok */
  67.  
  68.     if (term!=NULL && strcmp(term,TERM)!=0) {
  69.         fprintf(stderr,"%s only runs on %s terminals\n",name,TERM);
  70.         exit(1);
  71.        }
  72.  
  73.     /* get args */
  74.  
  75.    if (argc>1 && strcmp(argv[1],"-s")==0) {    /* don't redhape window */
  76.       argc--, argv++;
  77.       shape=0;
  78.       }
  79.  
  80.    if (argc>1)
  81.       cmd = argv[1];
  82.    else if ((cmd=getenv("CUT")) == NULL)
  83.       cmd = CMDNAME;
  84.  
  85.    /* make sure we can run the command */
  86.  
  87.    if ((test = popen(cmd,"w")) == NULL || pclose(test)!=0) {
  88.       fprintf(stderr,"%s: Can't start (%s)\n",name,cmd);
  89.       exit(1);
  90.       }
  91.    
  92.     /* set up window environment */
  93.  
  94.     m_setup(0);
  95.     m_ttyset();
  96.  
  97.     signal(SIGHUP,clean);
  98.     signal(SIGTERM,clean);
  99.     signal(SIGINT,clean);
  100.  
  101.     m_push(P_FONT|P_POSITION|P_EVENT|P_FLAGS);
  102.     get_size(&x,&y,&wide,&high);
  103.     get_param(0,0,0,&border);
  104.     m_setmode(M_NOWRAP);
  105.     m_func(B_COPY);
  106.  
  107.     /* get icons */
  108.  
  109.     if (!down_load(ICON_CLOSED,CLOSED,&w,&h)) {
  110.       fprintf(stderr,"%s: Can't find %s\n",name,ICON_CLOSED);
  111.         clean(1);
  112.         }
  113.     max_h = h; max_w = w;
  114.  
  115.     if (!down_load(ICON_OPEN,OPEN,&w,&h)) {
  116.       fprintf(stderr,"%s: Can't find %s\n",name,ICON_OPEN);
  117.         clean(1);
  118.         }
  119.     max_h = max(max_h,h);
  120.     max_w = max(max_w,w);
  121.  
  122.     if (!down_load(ICON_WINK,WINK,&w,&h)) {
  123.       fprintf(stderr,"%s: Can't find %s\n",name,ICON_WINK);
  124.         clean(1);
  125.         }
  126.  
  127.     max_h = max(max_h,h);
  128.     max_w = max(max_w,w);
  129.  
  130.     /* get icon position */
  131.    
  132.     m_font(1);    /* a small font */
  133.    if (shape) {
  134.         m_push(P_FLAGS|P_EVENT);
  135.         m_setmode(M_ABS);
  136.         m_setevent(BUTTON_1,"Z %p\r");
  137.         fprintf(stderr,"Click button 1 to indicate icon position\n");
  138.         while (*m_gets(line) != 'Z')
  139.             ;
  140.         sscanf(line,"Z %d %d",&w,&h);
  141.         ix = w + x + border; 
  142.         iy = h + y + border;
  143.         m_pop();
  144.  
  145.         /* iconify window */
  146.  
  147.         m_shapewindow(ix,iy,SLOP+2*border+max_w, SLOP+2*border+max_h);
  148.         dprintf(debug,"Shaping window to %d x %d\n",border+max_w, border+max_h);
  149.       }
  150.  
  151.     /* setup events */
  152.  
  153.     m_setevent(ACTIVATE,"A\r");
  154.     m_setevent(DEACTIVATE,"D\r");
  155.     m_setevent(REDRAW,"R\r");
  156.     m_setevent(RESHAPE,"S\r");
  157.     m_setevent(SNARFED,"C %c\r");
  158.     m_clearmode(M_ACTIVATE);
  159.  
  160.     /* display icon */
  161.  
  162.     setup(CLOSED);
  163.  
  164.     /* wait for an event */
  165.  
  166.     while(!feof(m_termin)) {
  167.         m_flush();
  168.         m_gets(line);
  169.         switch(*line) {
  170.             case 'A':    /* window is activated */
  171.             if (got) {
  172.                setup(OPEN);
  173.                    m_flush();
  174.                    m_put();
  175.                    m_sendme("\r\005 done\r");
  176.                    m_flush();
  177.                    append_file(cmd,got);
  178.                got = 0;
  179.                }
  180.             sleep(1);
  181.                 m_clearmode(M_ACTIVATE);
  182.                 break;
  183.             case 'D':    /* window is deactivated */
  184.                 setup(CLOSED);
  185.                 break;
  186.             case 'S':    /* window is reshaped */
  187.             if (shape) {
  188.                     get_size(&ix,&iy,0,0);
  189.                     m_shapewindow(ix,iy,SLOP+2*border+max_w, SLOP+2*border+max_h);
  190.                }
  191.                 setup(RESTORE);
  192.                 break;
  193.             case 'R':    /* screen is redrawn */
  194.             setup(RESTORE);
  195.                 break;
  196.             case 'C':    /* Someone cut something */
  197.                 got = atoi(line+2);        /* # of bytes */
  198.                 setup(WINK);
  199.                 m_flush();
  200.                 sleep(1);
  201.                 setup(CLOSED);
  202.             }
  203.         }
  204.     }
  205.  
  206. /*    Clean up and exit */
  207.  
  208. clean()
  209.     {
  210.     m_popall();
  211.     m_ttyreset();
  212.     exit(1);
  213.     }
  214.  
  215. /* setup an icon */
  216.  
  217. setup(where)
  218. int where;        /* bitmap # */
  219.     {
  220.    static int was=0;
  221.  
  222.    if (where==0)        /* restore previous icon */
  223.       where = was;
  224.  
  225.     m_clear();
  226.     m_bitcopyto(0,0,999,999,0,0,0,where);
  227.     dprintf(debug,"displaying icon [%d] \n",where);
  228.    m_flush();
  229.    was = where;
  230.     }
  231.  
  232. /* download icon */
  233.  
  234. int
  235. down_load(name,where,w,h)
  236. char *name;    /* name of icon */
  237. int where;    /* scratchpad bitmap # */
  238. int *w, *h;    /* icon size */
  239.     {
  240.     char buff[20];
  241.     int n;
  242.  
  243.    m_bitfromfile(where,name);
  244.    m_flush();
  245.    m_gets(buff);
  246.    n = sscanf(buff,"%d %d",w,h);
  247.     dprintf(debug,"Getting icon [%s] into %d (%d x %d)\n",
  248.         name,where,*w,*h);
  249.     if (n < 2) 
  250.         return(0);
  251.     else
  252.         return(1);
  253.     }
  254.  
  255. int
  256. append_file(cmd,bytes)
  257. char *cmd;        /* command to pipe file to */
  258. int bytes;
  259.     {
  260.    FILE *file;
  261.    long now = time(0);
  262.    char *ctime();
  263.     register int c,count = 0;
  264.     int gotn = 0;
  265.  
  266.    dprintf(stderr,"appending %d bytes\n",bytes);
  267.    file = popen(cmd,"w");
  268.    fprintf(file,"\n> %d bytes at: %s  ",bytes,ctime(&now));
  269.  
  270.     while((c=getc(m_termin)) != '\005') {
  271.         if (c=='\n' && gotn++ ==0) {
  272.            count++;
  273.             putc(c,file);
  274.             putc(' ',file);
  275.             putc(' ',file);
  276.             }
  277.         else if (c!= '\n') {
  278.             putc(c,file);
  279.             gotn = 0;
  280.             }
  281.         }
  282.     fflush(file);
  283.    pclose(file);
  284.     return(count);
  285.     }
  286.